package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sky.constant.RedisConstant.SET_MEAL_CACHE_KEY;

/**
 * 管理员端套餐管理
 */
@Api("套餐管理")
@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetMealController {
    @Autowired
    SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    /*
    * 将同一分类的菜品缓存删除
    * */
    @CacheEvict(cacheNames = SET_MEAL_CACHE_KEY,key="#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("套餐信息：{}",setmealDTO);
        setmealService.saveCategory(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询：{}",setmealPageQueryDTO);
        PageResult pageResult=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售、停售")
    /*
     * 清理所有的菜品缓存数据
     * 因为起售停售的是套餐，套餐中的菜品的种类不确定，所以全部清理
     * */
    @CacheEvict(cacheNames = SET_MEAL_CACHE_KEY,allEntries = true)
    public Result startOrStop(@PathVariable Long status,Long id){
        log.info("套餐起售停售：{}",status==0?"停售中":"起售中");
        setmealService.startOrStop(status,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("套餐id为：{}",id);
        SetmealVO setmealVO=setmealService.getById(id);
        return Result.success(setmealVO);
    }


    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    /*
    * 清理所有的菜品缓存数据
    * */
    @CacheEvict(cacheNames = SET_MEAL_CACHE_KEY,allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("待删除的套餐id:{}",ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改套餐")
    /*
     * 清理所有的菜品缓存数据
     * 因为可能商家会修改套餐的分类
     * */
    @CacheEvict(cacheNames = SET_MEAL_CACHE_KEY,allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改的套餐信息：{}",setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }
}
